package com.yufubao.service.service.applet.product.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yufubao.framework.util.page.Query;
import com.yufubao.framework.result.PageUtils;
import com.yufubao.model.dto.applet.product.SkuQueryPageDetailDto;
import com.yufubao.model.pojo.applet.product.SkuInfoEntity;
import com.yufubao.service.mapper.applet.product.SkuInfoDao;
import com.yufubao.service.service.applet.product.SkuInfoService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @param dto
     * @return com.yufubao.framework.result.PageUtils
     * @description 查询当前商店的sku信息
     * @author zhangyiheng
     * @date 2024/8/30 10:30
     */
    @Override
    public PageUtils queryPageDetail(SkuQueryPageDetailDto dto) {
        String key = dto.getKey();
        Long catelogId = dto.getCatelogId();
//        Object brandId = params.get("brandId");
        BigDecimal min = dto.getMin();
        BigDecimal max = dto.getMax();
        Long supplierInfoId = dto.getSupplierInfoId();

        LambdaQueryWrapper<SkuInfoEntity> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(key)) {
            wrapper.and(wrap -> {
                wrap.eq(SkuInfoEntity::getSkuId, key).or().like(SkuInfoEntity::getSkuName, key);
            });
        }

        wrapper.eq(!Objects.isNull(catelogId) && !Objects.equals(catelogId, "0"), SkuInfoEntity::getCatalogId, catelogId);
        wrapper.eq(SkuInfoEntity::getSupplierInfoId, supplierInfoId);
//        .eq(!Objects.isNull(brandId) && !Objects.equals(brandId, "0"), SkuInfoEntity::getBrandId, brandId);

        boolean minb = !Objects.isNull(min) && !Objects.equals(min, "0");
        boolean maxb = !Objects.isNull(max) && !Objects.equals(max, "0");
        if (minb && maxb) {
            wrapper.between(SkuInfoEntity::getPrice, min, max);
        } else {
            wrapper.eq(minb, SkuInfoEntity::getPrice, min)
                    .eq(maxb, SkuInfoEntity::getPrice, max);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(JSON.parseObject(JSON.toJSONString(dto),Map.class)),
                wrapper
        );


        return new PageUtils(page);
    }
}